package com.skivingcloud.apiplatform.mng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.skivingcloud.apiplatform.mng.entity.ApiPlatformMng;
import com.skivingcloud.apiplatform.mng.mapper.ApiPlatformMngMapper;
import com.skivingcloud.apiplatform.mng.service.IApiPlatformMngService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.UuidUtil;
import net.sf.json.JSONObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-11-01
 */
@Service
public class ApiPlatformMngServiceImpl extends ServiceImpl<ApiPlatformMngMapper, ApiPlatformMng> implements IApiPlatformMngService {

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        ApiPlatformMng apiPlatformMng = (ApiPlatformMng) JSONObject
                .toBean(JSONObject.fromObject(params.get("dataForm")), ApiPlatformMng.class);
        QueryWrapper<ApiPlatformMng> queryWrapper = new QueryWrapper<ApiPlatformMng>();
        if (apiPlatformMng != null) {
            if (StringUtils.isNotBlank(apiPlatformMng.getAppName())) {
                queryWrapper.like("app_name", "%" + apiPlatformMng.getAppName() + "%");
            }
            if (StringUtils.isNotBlank(apiPlatformMng.getAppId())) {
                queryWrapper.like("app_id", "%" + apiPlatformMng.getAppId() + "%");
            }
        }
        queryWrapper.orderByDesc(" mod_time ");
        List<ApiPlatformMng> list = this.baseMapper.selectList(queryWrapper);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    /**
     * 保存配置信息
     *
     * @param apiPlatformMng
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value="skivingcloud:api:apps",allEntries=true)
    public void save(ApiPlatformMng apiPlatformMng, String userId) {
        LocalDateTime nowDate = LocalDateTime.now();
        apiPlatformMng.setId(UuidUtil.get32UUID());
        apiPlatformMng.setCreateTime(nowDate);
        apiPlatformMng.setModTime(nowDate);
        apiPlatformMng.setCreater(userId);
        apiPlatformMng.setModUser(userId);
        this.baseMapper.insert(apiPlatformMng);
    }

    /**
     * 更新配置信息
     *
     * @param apiPlatformMng
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value="skivingcloud:api:apps",allEntries=true)
    public void update(ApiPlatformMng apiPlatformMng, String userId) {
        LocalDateTime nowDate = LocalDateTime.now();
        apiPlatformMng.setModTime(nowDate);
        apiPlatformMng.setModUser(userId);
        this.baseMapper.updateById(apiPlatformMng);
    }

    /**
     * 删除配置信息
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value="skivingcloud:api:apps",allEntries=true)
    public void deleteBatch(String[] ids) {
        this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 主键获取
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public ApiPlatformMng selectById(String id) throws Exception {
        return this.baseMapper.selectById(id);
    }

    /**
     * 根据appId查询
     *
     * @param appId
     * @return
     */
    @Override
    @Cacheable(value="skivingcloud:api:apps",key="#appId + '_selectByAppId'")
    public ApiPlatformMng selectByAppId(String appId) {
        QueryWrapper<ApiPlatformMng> queryWrapper = new QueryWrapper<ApiPlatformMng>();
        queryWrapper.eq("app_id", appId);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
